// pages/order/addfee/addfee.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPicker: false,
    packList: [{
      id: 1,
      name: "车费",
      text: "车费"
    }, {
      id: 2,
      name: "人工费",
      text: "人工费"
    }, {
      id: 3,
      name: "其他费用",
      text: "其他费用"
    }],
    selectedPackItem: {},
    feeName: "",
    otherName: "",
    feePrice: "",
    errMessage: {
      feeName: "请填写费用名称",
      feePrice: "请填写费用价格",
      // otherFee: "请填写其他费用名称"
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShareAppMessage: function () {

  },
  picker() {
    this.setData({
      showPicker: true
    })
  },
  changename(e) {
    const name = e.detail;
    this.setData({
      otherName: name
    })
    console.log(name, 'name')

  },
  changeprice(e) {
    const price = e.detail;
    this.setData({
      feePrice:this.money(price) 
    })
		console.log(this.data.feePrice,'价格')

  },
	money(val) {
		let num = val.toString(); //先转换成字符串类型
		if (num.indexOf('.') == 0) { //第一位就是 .
			num = '0' + num
		}
		num = num.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
		num = num.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
		num = num.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
		num = num.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
		if (num.indexOf(".") < 0 && num != "") {
			num = parseFloat(num);
		}
		return num
	},
  onConfirm(event) {
    const {
      value
    } = event.detail;
    this.setData({
      selectedPackItem: value,
      showPicker: false
    })
  },
  onCancel() {
    this.setData({
      showPicker: false
    })
  },
  submit() {
    if (this.data.selectedPackItem.name == "其他费用") {
      this.setData({
        feeName: this.data.otherName
      })
    } else {
      this.setData({
        feeName: this.data.selectedPackItem.name,
      })
    }
    const params = {
      feeName: this.data.feeName,
      feePrice: this.data.feePrice,
    }
    for (let key in params) {
      if (!params[key]) {
        wx.showToast({
          title: this.data.errMessage[key],
          icon: 'none',
          duration: 2000
        })
        return
      }
    }
    wx.setStorage({
      key: "feeData",
      data: params,
      success: function () {
        wx.navigateBack({
          delta: 1
        }); //返回上一个页面
      }
    })
  },
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */

})